package com.iot08.api.service;

import com.aliyun.tea.utils.StringUtils;
import com.iot08.api.response.WallpaperCategoryResp;
import com.iot08.api.vo.request.WallpaperCategoryAddEditReqVo;
import com.iot08.common.entity.app.Wallpaper;
import com.iot08.common.entity.app.WallpaperCategory;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description 壁纸分类信息
 * @date 2025/3/10 18:12
 **/
@Slf4j
@Service
public class WallpaperCategoryService extends BaseAdminService<WallpaperCategory> {

    @Resource
    private WallpaperService wallpaperService;

    /**
     * @description 查询壁纸分类信息
     * @author cwj
     * @param []
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/11 16:53
     **/
    public ResponseMap queryWallpaperCategory() {
        Query query = new Query(Criteria.where("status").is(1));
        List<WallpaperCategoryResp> wallpaperCategoryResps = mongoTemplate.find(query,WallpaperCategoryResp.class,"wallpaper_category");
        ResponseMap res = ResponseMap.success();
        res.put("wallpaperCategoryResps",wallpaperCategoryResps);
        return res;
    }


    /**
     * @description 增加壁纸分类信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/12 10:20
     **/
    public ResponseMap addWallpaperCategory(WallpaperCategoryAddEditReqVo vo) {
        if(StringUtils.isEmpty(vo.getName())) {
            return ResponseMap.fail("参数缺失，壁纸分类名称不能为空");
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(vo.getName())).limit(1);
        WallpaperCategory wallpaperCategory = this.findOne(query);
        if(ObjectUtils.isNotEmpty(wallpaperCategory)) {
            return ResponseMap.fail("已存在同名的分类信息，添加失败");
        }
        wallpaperCategory = new WallpaperCategory();
        wallpaperCategory.setName(vo.getName());
        wallpaperCategory.setStatus(ObjectUtils.isEmpty(vo.getStatus())?1: vo.getStatus());
        wallpaperCategory.setIsDefault(ObjectUtils.isEmpty(vo.getIsDefault())?0:vo.getIsDefault());
        wallpaperCategory.setDesc(vo.getDesc());
        wallpaperCategory.setCreateTime(new Date());
        wallpaperCategory.setUpdateTime(new Date());
        this.save(wallpaperCategory);
        return ResponseMap.success();
    }


    /**
     * @description 修改壁纸分类信息
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/12 10:20
     **/
    public ResponseMap editWallpaperCategory(WallpaperCategoryAddEditReqVo vo) {
        if(StringUtils.isEmpty(vo.getCid())){
            return ResponseMap.fail("参数缺失，壁纸分类id不能为空");
        }
        Update update = new Update();
        if(!StringUtils.isEmpty(vo.getName())) {
            //修改后的名字有没有重名
            Query query = new Query();
            query.addCriteria(Criteria.where("name").is(vo.getName()).and("_id").ne(vo.getCid())).limit(1);
            WallpaperCategory wallpaperCategory = this.findOne(query);
            if(ObjectUtils.isNotEmpty(wallpaperCategory)){
                return ResponseMap.fail("已存在同名的分类信息，修改失败");
            }
            update.set("name",vo.getName());
        }
        update.set("updateTime",new Date());
        if(ObjectUtils.isNotEmpty(vo.getStatus())){
            if(vo.getStatus().equals(2)){
                //检查有没有壁纸使用这个分类，如果使用则不允许作废或删除
                Wallpaper wallpwaper = wallpaperService.queryWallPaperByCategoryId(vo.getCid());
                if(ObjectUtils.isNotEmpty(wallpwaper)) {
                    return ResponseMap.fail("此分类信息下仍然存在壁纸，无法作废或删除");
                }
            }
            update.set("status",vo.getStatus());
        }
        if(ObjectUtils.isNotEmpty(vo.getIsDefault())){
            update.set("isDefault",vo.getIsDefault());
        }
        if(!StringUtils.isEmpty(vo.getDesc())) {
            update.set("desc",vo.getDesc());
        }
        this.updateById(new ObjectId(vo.getCid()),update);
        if(!StringUtils.isEmpty(vo.getName())) {
            wallpaperService.updateWallPaperByCategoryId(vo.getCid(),vo.getName());
        }
        return ResponseMap.success();
    }

}
